<template>
	<view>
		<u-navbar :border-bottom="false" title="企业注册" :title-size="36" :title-bold="true" :title-color="textColor">
			<view v-if="showNext" slot="right" class="u-m-r-35" @click="modalShow = true">
				<text class="text-bold u-font-28">跳过</text>
			</view>
		</u-navbar>
		<u-form class="form" :model="form" ref="registerMdForm" :error-type="['toast']">
			<u-form-item label-position="top" label="简称" :label-style="labelStyle" prop="abbreviation" required>
				<u-input v-model="form.abbreviation" placeholder="请输入店铺简称" />
			</u-form-item>
			<u-form-item label-position="top" label="经营地址" :label-style="labelStyle" prop="regionId" required
				right-icon="forward">
				<u-input type="select" :select-open="pickerShow" v-model="region" placeholder="省/市/区（县）"
					@click="pickerShow = true" />
			</u-form-item>
			<u-form-item label-position="top" label="经营地址详情" :label-style="labelStyle" prop="address" required>
				<u-input v-model="form.address" placeholder="请输入经营地址" />
				<image src="/static/home/dingwei.png" style="width: 30rpx;height:36rpx;" mode=""
					@click="chooseLocation"></image>
			</u-form-item>
			<!-- <u-form-item label-position="top" label="门店类型" :label-style="labelStyle" prop="shopType" required
				right-icon="forward">
				<u-input type="select" :select-open="typeShow" v-model="shopTypeName" placeholder="请选择门店类型"
					@click="typeShow = true" />
			</u-form-item> -->
			<u-form-item label-position="top" label="营业执照" prop="businessLicenseResourceId" :label-style="labelStyle"
				required>
				<!-- <u-input placeholder="点击上传识别" :disabled="true" /> -->
				<u-upload ref="avatarUploadRef"  :action="baseUrl + 'api/Resource/Upload'"
					:file-list="fileList" :header="header" :max-count="1" name="image" :deletable="true"
					:show-progress="false" @on-success="onSuccess" @on-remove="onRemove" width="160" height="160">
				</u-upload>
			</u-form-item>
			<u-form-item  label="执照编号" label-width="76px" prop="unifiedSocialCreditCode" required>
				<u-input v-model="form.unifiedSocialCreditCode" placeholder="请输入营业执照编号" />
			</u-form-item>
			<u-form-item  label="法人代表" label-width="76px" prop="legalPersonName" required>
				<u-input v-model="form.legalPersonName" placeholder="请输入法人代表名称" />
			</u-form-item>
			<u-form-item  label="企业名称" label-width="76px" prop="name" required>
				<u-input v-model="form.name" placeholder="请输入企业名称" />
			</u-form-item>
			<u-form-item  label="注册地址" label-width="76px" prop="registerAddress" required>
				<u-input v-model="form.registerAddress" placeholder="请输入企业注册地址(营业执照上)" />
			</u-form-item>
			<u-form-item  label="营业期限" label-width="76px" prop="businessTime" required>
				<u-input v-model="form.businessTime" placeholder="请输入'营业执照有效期'或'长期'" />
			</u-form-item>
		</u-form>
		<set-submit-bt v-if="isShopEdit||form.state == 4" text="提交审核" @click="handleSubmit" />
		<!-- 门店类型 -->
		<u-select mode="single-column" v-model="typeShow" :list="$store.state.shop.shopType" @confirm="chooseType">
		</u-select>
		<!-- 地区选择 -->
		<u-picker mode="region" v-model="pickerShow" :confirm-color="themeColor" :default-region="defaultRegion" :area-code="defaultArea" @confirm="regionConfirm"></u-picker>
		<!-- modal框 -->
		<u-modal v-model="modalShow" title="您确定跳过注册门店？" :title-style="{fontWeight: 'bold'}"
			:content-style="contentStyle" :show-cancel-button="true" :confirm-color="themeColor" confirm-text="是"
			cancel-text="否" @confirm="$mRouter.switchTab({ route: '/pages/mine/index'})">
			<view class="slot-content">
				<rich-text :nodes="modalContent"></rich-text>
			</view>
		</u-modal>
		<akLoading isFullScreen :active="loading"></akLoading>
	</view>
</template>

<script>
	import SetSubmitBt from '@/components/set-submit-bt/index.vue'
	export default {
		components: {
			SetSubmitBt
		},
		data() {
			return {
				typeShow: false,
				modalShow: false,
				showNext: false,
				loading:false,
				submitStyle: {
					height: '100rpx',
					color: '#FFFFFF',
					background: this.linearGradient
				},
				modalContent: `
					开店后您可以以批发价进货哦~<br>
					您也可以在“我”的“设置”中注册门店
				`,
				contentStyle: {
					color: this.greyColor,
					textAlign: 'center',
					lineHeight: '50rpx',
					fontSize: '22rpx',
					padding: '15rpx 0'
				},
				labelStyle: {
					fontSize: '30rpx',
					fontWeight: 'bold',
					width:'106px'
				},
				header: {
					Authorization: 'Bearer ' + this.$store.state.user.access_token,
				},
				fileList: [],
				shopTypeName: '',
				region: '',
				form: {
					name: '',
					businessLicenseResourceId: '',
					regionId: 0,
					managerId: this.$store.state.user.user.id,
					address: '',
					shopType: 0,
					shopLevelId: ''
				},
				rules: {
					abbreviation: [{
						required: true,
						message: '店铺简称不能为空',
						trigger: 'blur'
					}],
					name: [{
						required: true,
						message: '店铺名称不能为空',
						trigger: 'blur'
					}],
					unifiedSocialCreditCode: [{
						required: true,
						message: '营业执照编号不能为空',
						trigger: 'blur'
					}],
					legalPersonName: [{
						required: true,
						message: '法人不能为空',
						trigger: 'blur'
					}],
					registerAddress: [{
						required: true,
						message: '营业执照注册地址不能为空',
						trigger: 'blur'
					}],
					businessTime: [{
						required: true,
						message: '营业期限不能为空',
						trigger: 'blur'
					}],
					businessLicenseResourceId: [{
						required: true,
						message: '营业执照必须上传',
						trigger: 'blur'
					}],
					regionId: [{
						required: true,
						message: '省市区必须选择',
						trigger: 'blur'
					}],
					address: [{
						required: true,
						message: '经营地址不能为空',
						trigger: 'blur'
					}],
					// shopType: [{
					// 	required: true,
					// 	message: '店铺类型必须选择',
					// 	trigger: 'blur'
					// }]
				},
				pickerShow: false,
				defaultArea:[],
				defaultRegion:[],
				isShopEdit:true,
			}
		},
		onLoad(options) {
			// var pages = getCurrentPages(); //当前页
			// var beforePage = pages[pages.length - 2].route; //上个页面
			// 如果是第一次注册进来 显示跳过按钮
			// if (beforePage == 'pages/wo/profile') {
			// 	this.showNext = true
			// }
			// 获取店铺等级
			// this.getShopLevel()
			if(options.type== 'edit'){
				this.getApplyInfoFn(options.id);
				this.isShopEdit = false;
			}
			if(options.type== 'saleAdd'){
				this.form.managerId = options.userId;
				this.form.salesmanId = this.$store.state.user.user.id;
			}
		},
		onReady() {
			this.$refs.registerMdForm.setRules(this.rules)
		},
		methods: {
			// 图片上传成功触发
			onSuccess(data) {
				if(data.code== 200){
					this.form.businessLicenseResourceId = data.data[0].id;
					uni.showLoading({
						title: '营业执照识别中',
					});
					this.getBussinesInfoFn(data.data[0].id);
					uni.hideLoading();
				}else{
					this.$refs.avatarUploadRef.handlerDeleteItem(0);
				}
			},
			// 图片移除时触发
			onRemove() {
				this.form.businessLicenseResourceId = ''
			},
			// 选择门店类型
			chooseType(e) {
				this.shopTypeName = e[0].label
				this.form.shopType = e[0].value.toString()
			},
			// 选择地区回调
			regionConfirm(e) {
				console.log(e)
				this.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
				this.form.regionId = e.area.value;
				this.form.cityId = e.city.value+'00';
				this.form.provinceId = e.province.value+'0000';
				console.log(this.form);
			},
			// 定位
			chooseLocation() {
				uni.chooseLocation({
					success: (res) => {
						this.form.address = res.address
					}
				});
			},
			// 营业执照识别
			async getBussinesInfoFn(id){
				this.loading = true;
				let params = {
					resourceId:id
				};
				const res = await this.$newHttp.get('api/ThirdParty/ali/recognizeBusinessLicense',{params:params});
				this.loading = false;
				if(res.code == 200){
					this.form.name = res.data.name;
					this.form.unifiedSocialCreditCode = res.data.registerNumber;
					this.form.legalPersonName = res.data.legalPerson;
					this.form.registerAddress = res.data.address;
					// this.form.businessTime = this.insertStr(res.data.establishDate,4,'-')+'至'+this.insertStr(res.data.validPeriod,4,'-');
					this.form.businessTime = res.data.validPeriod;
					
				}else{
					this.$u.toast('营业执照识别失败')
				}
			},
			// 时间日期标准化
			insertStr(source, start, newStr) {
				let a = source.slice(0, start);
				let b = source.slice(start,start+2);
				let c = source.slice(start+2);
				// return source.slice(0, start) + newStr + source.slice(start);
				return a+newStr+b+newStr+c;
			},
			// 获取店铺等级
			async getShopLevel() {
				const res = await this.$http.get('api/Shop/GetAllShopLevels')
				const arr = res.find(item => {
					return item.name == '门店'
				})
				this.form.shopLevelId = arr.id
			},
			// 获取申请的信息
			async getApplyInfoFn(id){
				this.loading = true;
				let params = {
					shopId:id
				}
				const res = await this.$newHttp.get('api/Shop/GetSignupInfo',{params:params});
				this.loading = false;
				if(res.code === 200){
					this.form = res.data;
					// let index = res.data.shopType -1;
					// this.shopTypeName = this.$store.state.shop.shopType[index].label
					// this.form.shopType = this.$store.state.shop.shopType[index].value.toString();
					this.fileList = [
						{
							url: res.data.url,
						}
					];
					let regionCode = res.data.regionId;
					this.defaultArea = [String(regionCode).substr(0, 2), String(regionCode).substr(0, 4), String(regionCode)];
					this.defaultRegion = [res.data.provinceName,res.data.cityName,res.data.regionName];
					this.region = res.data.provinceName+'-'+res.data.cityName+'-'+res.data.regionName
					this.form.regionId =regionCode.toString();
				}
				
				// $store.state.shop.shopType
			},
			handleSubmit() {
				uni.showLoading({
					title: 'loading...',
					mask: false
				});
				
				this.$refs.registerMdForm.validate(async (valid) => {
					if (!valid){
						uni.hideLoading({
							title: 'loading...',
							mask: false
						});
						return false;
					} 
					const res = await this.$newHttp.post('api/Shop/Signup', this.form);
					uni.hideLoading({
						title: 'loading...',
						mask: false
					});
					if (res.code == 200) {
						this.$u.toast('注册成功', 1000, 'success')
						this.$mHelper.delayedJump('/pages/mine/index', 'switchTab')
					}else{
						this.$u.toast('注册失败'+res.erroMessage, 1000)
					}
				})
			}
		}
	}
</script>

<style>
	.page {
		background-color: #FFFFFF;
	}
</style>

<style lang="scss" >
	.container {
		// padding: 0 70rpx;
	}

	.form {
		padding: 0 70rpx;
		
		
	}
</style>
